package club.justwill.single.Service.impl;

import club.justwill.single.Service.SkuService;
import club.justwill.single.assembler.SkusAssemble;
import club.justwill.single.bean.SearchSkus;
import club.justwill.single.bean.SkuSupportResp;
import club.justwill.single.helper.FileHelper;
import club.justwill.single.impl.ToolsImpl;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.persistence.dao.ProjectDao;
import club.justwill.single.persistence.dao.SkuDao;
import club.justwill.single.persistence.document.SkuSupport;
import club.justwill.single.persistence.jpo.Projects;
import club.justwill.single.persistence.jpo.Skus;
import club.justwill.single.persistence.store.SkuSupoortStore;
import club.justwill.single.util.JsonResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luoyu on 1/1/16.
 */
@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class SkuServiceImpl extends BaseService implements SkuService {
    @Autowired
    private SkuDao skusDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private FileHelper fileHelper;

    @Autowired
    private SkuSupoortStore skuSupoortStore;

    @Autowired
    private SkusAssemble skusAssemble;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public List<Skus> searchSkusByProjectId(SecurityToken token, long projectId, JsonResponse obj) {

            List<Skus> skusList = skusDao.getSkusByProjectId(projectId);
            if(skusList == null){
                JsonResponseHelper.addError(obj,CoreConstants.SKU_IS_EMPTY);
                return new ArrayList<Skus>();
            }else if(skusList.size() == 0){
                JsonResponseHelper.addError(obj,CoreConstants.SKU_IS_EMPTY);
            }
            return skusList;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public void saveSkuFile(SecurityToken token, JsonResponse obj, CommonsMultipartFile cover, Long skuId) {
        String path = fileHelper.saveFile(cover);
        if(path == null || "".equals(path)){
            JsonResponseHelper.addError(obj, CoreConstants.FILE_UPLOAD_FAILED);
        }else{
            Skus skus = skusDao.getBySkuId(skuId);
            if(skus!=null && skus.getSkusId()!=0){
                skus.setSkuUrl(path);
                skusDao.update(skus,token);
            }
        }
    }

    @Override
    public List<SkuSupportResp> searchSkuSupportByProjectId(JsonResponse obj, long projectId) {
        List<SkuSupport> skuSupportList = skuSupoortStore.searchSkuSupportByProject(projectId);
        if (skuSupportList == null) {
            return new ArrayList<SkuSupportResp>();
        }
        List<SkuSupportResp> skuSupportRespList= skusAssemble.assebleSkuSupportList(skuSupportList);
        return skuSupportRespList;
    }
}
